package cn.ittaoli.szdz.service.Impl;

import cn.ittaoli.szdz.dao.DzJbxxMapper;
import cn.ittaoli.szdz.dao.VDzBmzsMapper;
import cn.ittaoli.szdz.model.DzJbxx;
import cn.ittaoli.szdz.model.VDzBmzs;
import cn.ittaoli.szdz.service.SingleService;
import cn.ittaoli.utils.ExcelUtils;
import cn.ittaoli.utils.FileUpload;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by lub_lenovo on 2017/8/18.
 */
@Service
public class SingleServiceImpl implements SingleService {
    @Autowired
    private VDzBmzsMapper vDzBmzsMapper;

    @Autowired
    private DzJbxxMapper dzJbxxMapper;

    @Override
    public List<VDzBmzs> selectAll() {
        return vDzBmzsMapper.selectAll();

    }

    @Override
    public List<VDzBmzs> selectSQBmzs() {
        return vDzBmzsMapper.selectSQBmzs();
    }

    @Override
    public Integer upExcel(MultipartFile file, HttpServletRequest request) throws IOException {
        String path= FileUpload.upload(file,request);
        String absPath=request.getSession().getServletContext().getRealPath("")+path;

        Integer result=0;

        Map<String, List<HSSFRow>> map = ExcelUtils.importExcel(absPath);

        for (List<HSSFRow> hssfRows:map.values()){
            for (HSSFRow row:hssfRows){

                DzJbxx dzJbxx=new DzJbxx();

//                 "id","考生号","语文成绩","数学成绩","外语成绩"};
                String id = ExcelUtils.getValue(row.getCell(0));
                dzJbxx.setKsh(Long.parseLong(ExcelUtils.getValue(row.getCell(1))));
                dzJbxx.setXm(ExcelUtils.getValue(row.getCell(2)));
                dzJbxx.setYwcj(ExcelUtils.transformType(ExcelUtils.getValue(row.getCell(3))));
                dzJbxx.setSxcj(ExcelUtils.transformType(ExcelUtils.getValue(row.getCell(4))));
                dzJbxx.setWycj(ExcelUtils.transformType(ExcelUtils.getValue(row.getCell(5))));

                result+=dzJbxxMapper.updateSelectiveByKsh(dzJbxx);
            }

        }

        return result;
    }

    @Override
    public List<DzJbxx> selectAllJbxx() {

        return dzJbxxMapper.selectAllJbxx();
    }
}
